package mwa.cs.dao;

import java.util.List;
import java.util.Map;

import mwa.cs.database.Database;

/**
 * A class used for managing news
 */
public class NewsDAO {
	private Database db;
	
	 public NewsDAO(Database db) {
	        this.db = db;
	    }
	 //get news by site_id
	 public List<Map<String, Object>> newsTypeS(int pnSiteId,String paNowDate) {
	        String vaSql = "select topic,detail,IMAGE_URL from PRESS_RELEASE "
	        		    + " WHERE site_id ="+pnSiteId +""
	        		    + " AND start_date <="+paNowDate+ " "
	        		    + " AND end_date >="+paNowDate+ " "
	        			+ " ORDER BY PIN DESC , START_DATE DESC";
	        List<Map<String, Object>> result = db.queryList(vaSql);
	      
	        if (result != null) {
	            return result;
	        } else {
	            return null;
	        }
	    }
	 
	 //get new type 'P'
	 public List<Map<String, Object>> newsTypeP(int pnSiteId,String paNowDate) {
	        String vaSql = "select topic,detail,IMAGE_URL from PRESS_RELEASE "
	        		    + " WHERE ROWNUM <= 3  "
	        		    + " AND site_id = 0 "
	        		    + " AND start_date <="+paNowDate+ " "
	        		    + " AND end_date >="+paNowDate+ " "
	        			+ " ORDER BY PIN DESC , START_DATE DESC";
	        List<Map<String, Object>> result = db.queryList(vaSql);
	        if (result != null) {
	            return result;
	        } else {
	            return null;
	        }
	    }
	 
	 //get detail by topic and nowDate for type'S'
	 public Map<String, Object> findDetailByTopic(int pnSiteId, String paTopic,String paNowDate) {
	        String vaSql = "SELECT  detail  FROM press_release"
	        		+ " WHERE site_id = " + pnSiteId + " "
	        		+ " AND start_date <="+paNowDate+ " "
	        		+ " AND end_date >="+paNowDate+ " "
	        		+ " AND topic like '" + paTopic + "%' ";
	        Map<String, Object> result = db.querySingle(vaSql);
	        if (result != null) {
	            return  result;
	        } else {
	            return null;
	        }
	    }
	
	//get detail by topic and nowDate for type'P'
		 public Map<String, Object> findDetailByTopicTypeP(int pnSiteId, String paTopic,String paNowDate) {
		        String vaSql = "SELECT  detail  FROM press_release"
		        		+ " WHERE site_id = 0 "
		        		+ " AND start_date <="+paNowDate+ " "
		        		+ " AND end_date >="+paNowDate+ " "
		        		+ " AND topic like '" + paTopic + "%' ";
		        Map<String, Object> result = db.querySingle(vaSql);
		        if (result != null) {
		            return  result;
		        } else {
		            return null;
		        }
		    }
		 


}
